package main

import (
	"encoding/json"
	"errors"
	"io/ioutil"
	"net/http"
	"strconv"

	"github.com/thedevsaddam/gojsonq"
	"github.com/wtf/mahjong/utils"
)

type loadLevelData struct {
	Level int `json:"level"`
}

func loadLevel(write http.ResponseWriter, request *http.Request) {
	body, err := ioutil.ReadAll(request.Body)
	defer request.Body.Close()
	utils.CheckErr(write, err, 100, "loadLevel:没有收到登录数据")
	if err != nil {
		return
	}

	var loadLevelD loadLevelData
	err1 := json.Unmarshal(body, &loadLevelD)

	utils.CheckErr(write, err1, 101, "loadLevel:无法解析登录的json数据")
	if err1 != nil {
		return
	}

	var level = loadLevelD.Level
	var level_key string
	if level < 31 {
		utils.CheckErr(write, errors.New("关卡无需获取"), 102, "loadLevel:关卡无需获取")
		return
	}

	// 判断最后关卡
	if level > 435 {
		// 151-165  421-435
		temp := getTrueLevel(level)
		if temp == 0 {
			level = 435
		} else {
			level = 150 + temp
		}
	}

	if level >= 31 && level <= 45 {
		level_key = "31-45"
	} else if level >= 46 && level <= 60 {
		level_key = "46-60"
	} else if level >= 61 && level <= 75 {
		level_key = "61-75"
	} else if level >= 76 && level <= 90 {
		level_key = "76-90"
	} else if level >= 91 && level <= 105 {
		level_key = "91-105"
	} else if level >= 106 && level <= 120 {
		level_key = "106-120"
	} else if level >= 121 && level <= 135 {
		level_key = "121-135"
	} else if level >= 136 && level <= 150 {
		level_key = "136-150"
	} else if level >= 151 && level <= 165 {
		level_key = "151-165"
	} else if level >= 166 && level <= 180 {
		level_key = "166-180"
	} else if level >= 181 && level <= 195 {
		level_key = "181-195"
	} else if level >= 196 && level <= 210 {
		level_key = "196-210"
	} else if level >= 211 && level <= 225 {
		level_key = "211-225"
	} else if level >= 226 && level <= 240 {
		level_key = "226-240"
	} else if level >= 241 && level <= 255 {
		level_key = "241-255"
	} else if level >= 256 && level <= 270 {
		level_key = "256-270"
	} else if level >= 271 && level <= 285 {
		level_key = "271-285"
	} else if level >= 286 && level <= 300 {
		level_key = "286-300"
	} else if level >= 301 && level <= 315 {
		level_key = "301-315"
	} else if level >= 316 && level <= 330 {
		level_key = "316-330"
	} else if level >= 331 && level <= 345 {
		level_key = "331-345"
	} else if level >= 346 && level <= 360 {
		level_key = "346-360"
	} else if level >= 361 && level <= 375 {
		level_key = "361-375"
	} else if level >= 376 && level <= 390 {
		level_key = "376-390"
	} else if level >= 391 && level <= 405 {
		level_key = "391-405"
	} else if level >= 406 && level <= 420 {
		level_key = "406-420"
	} else if level >= 421 && level <= 435 {
		level_key = "421-435"
	} else {
		utils.CheckErr(write, errors.New("没有更多关卡"), 103, "loadLevel:没有更多关卡")
		return
	}

	levels := "levels/" + level_key + ".json"
	levelData := gojsonq.New().File(levels).Find(strconv.Itoa(level))

	returnDa := utils.ReturnData{
		Errcode: 0,
		Errmsg:  "is ok",
		Data:    levelData,
	}
	returnDa.Returnfunc(write)
}

func getTrueLevel(level int) (newLevel int) {
	newLevel = (level - 435) % 285
	return
}
